// 相加
Handlebars.registerHelper ("mccmsaAdd", function (value1, value2) {
    return value1 + value2;
}); 

// 粗体
Handlebars.registerHelper ("mccmsaBold", function (value) {
    return new Handlebars.SafeString("<b>" + value + "</b>");
});

// 相等则显示
Handlebars.registerHelper ("mccmsaShowIfEqual", function (value, value2, options) {
    if(value == value2){
        return options.fn(this);
    }

    return options.inverse(this);
});

// 加减乘除运算
Handlebars.registerHelper ("mccmsMath", function (value1, value2, operator) {
    var result;

    switch (operator) {
        case "+":
            result = value1 + value2;
            break;
        case "-":
            result = value1 - value2;
            break;
        case "*":
            result = value1 * value2;
            break;
        case "/":
            result = value1 / value2;
            break;
    }

    return result;
});

// 下拉框选择
Handlebars.registerHelper('mccmsSelect', function( value, options ){
    var element = $('<select />').html( options.fn(this) );
    element.find('[value="' + value + '"]').attr({'selected':'selected'});
    return element.html();
});

// 单选框选择
Handlebars.registerHelper ("mccmsChecked", function (value, currentValue) {
    if ( value == currentValue ) {
        return "checked"
    } else {
        return "";
    }
});
// 判断传值为true返回selected
Handlebars.registerHelper ("mccmsSelectedE", function (value, currentValue) {
    if ( value == true ) {
        return "selected"
    } else {
        return "";
    }
});

// select选中判断
Handlebars.registerHelper('isSelect', function(result, options) {
    var htmlStr = '';
    for(var i = 0; i < result.ntAuthorities.length; i++){
        var item = result.ntAuthorities[i];
        var flag = false;
        if (result.ntRoles) {
            for(var j = 0; j < result.ntRoles.ntAuthorities.length; j++){
                if (result.ntRoles) {
                    var itemJ = result.ntRoles.ntAuthorities[j];
                    if(item.id == itemJ.id){
                        flag = true;
                        break;
                    }
                }else{
                    break;
                }
            }
        }
        if(flag){
            htmlStr += '<option value="' + item.id + '" selected>' + item.name + '</option>';
        }else{
            htmlStr += '<option value="' + item.id + '">' + item.name + '</option>';
        }
    }

  return new Handlebars.SafeString(htmlStr);
});

// 频道列表频道类型select选中判断
Handlebars.registerHelper('isSelectType', function(result, options) {
    var htmlStr = '';
    if(result.flag) {
        for (var i = 0; i < result.ntAuthorities.length; i++) {
            var item = result.ntAuthorities[i];
            var flag = false;
            if (result.syChannelinfo.ntAuthorities) {
                var itemJ = result.syChannelinfo.ntAuthorities;
                if (item.id == itemJ.id) {
                    flag = true;
                }
            }
            if (flag) {
                htmlStr += '<option value="' + item.id + '" selected>' + item.name + '</option>';
            } else {
                htmlStr += '<option value="' + item.id + '">' + item.name + '</option>';

            }
        }
    }else {
        for (var j = 0; j < result.ntAuthorities.length; j++){
            var authorityItem = result.ntAuthorities[j];
            htmlStr += '<option value="' + authorityItem.id + '">' + authorityItem.name + '</option>';
        }
    }
    return new Handlebars.SafeString(htmlStr);
});

// 频道列表上级频道select选中判断
Handlebars.registerHelper('isSelectSuperior', function(result, options) {
    var htmlStr = '';
    if(result.flag) {
        if(result.syChannelinfo.syChannelinfo == null){
            htmlStr += '<option value="">一级频道</option>';
        }
        for (var i = 0; i < result.superiorSyChannels.length; i++) {
            var item = result.superiorSyChannels[i];
            var flag = false;
            if (result.syChannelinfo.syChannelinfo) {
                var itemJ = result.syChannelinfo.syChannelinfo;
                if (item.id == itemJ.id) {
                    flag = true;
                }
            }
            if (flag) {
                htmlStr += '<option value="' + item.id + '" selected>' + item.channelname + '</option>';
            } else {
                htmlStr += '<option value="' + item.id + '">' + item.channelname + '</option>';

            }
        }
    }
    else {
        htmlStr += '<option value="">一级频道</option>';
        for (var j = 0; j < result.superiorSyChannels.length; j++){
            var authorityItem = result.superiorSyChannels[j];
            htmlStr += '<option value="' + authorityItem.id + '">' + authorityItem.channelname + '</option>';
        }
    }
    return new Handlebars.SafeString(htmlStr);
});

// 直/点播视频列表所属频道select选中判断
Handlebars.registerHelper('isSelectChannel', function(result, options) {
    var htmlStr = '';
    if(result.flag) {
        for (var i = 0; i < result.syChannelinfos.length; i++) {
            var item = result.syChannelinfos[i];
            var flag = false;
            if (result.syPlayInfo.syChannelinfo) {
                var itemJ = result.syPlayInfo.syChannelinfo;
                if (item.id == itemJ.id) {
                    flag = true;
                }
            }
            if (flag) {
                htmlStr += '<option value="' + item.id + '" selected>' + item.channelname + '</option>';
            } else {
                htmlStr += '<option value="' + item.id + '">' + item.channelname + '</option>';

            }
        }
    }
    else {
        for (var j = 0; j < result.length; j++){
            var authorityItem = result[j];
            htmlStr += '<option value="' + authorityItem.id + '">' + authorityItem.channelname + '</option>';
        }
    }
    return new Handlebars.SafeString(htmlStr);
});

// 直/点播视频列表所属频道select选择项
Handlebars.registerHelper('selectChannel', function (result, options) {
    var htmlStr = '<option value="">所有频道</option>';
    for (var i = 0; i < result.length; i++) {
        var item = result[i];
        htmlStr += '<option value="' + item.id + '">' + item.channelname + '</option>';
    }
    return new Handlebars.SafeString(htmlStr);
});

// 直/点播视频列表所属频道select选中判断
Handlebars.registerHelper('isSelectChannel', function(result, options) {
    var htmlStr = '';
    if(result.flag) {
        for (var i = 0; i < result.syChannelinfos.length; i++) {
            var item = result.syChannelinfos[i];
            var flag = false;
            if (result.syPlayInfo.syChannelinfo) {
                var itemJ = result.syPlayInfo.syChannelinfo;
                if (item.id == itemJ.id) {
                    flag = true;
                }
            }
            if (flag) {
                htmlStr += '<option value="' + item.id + '" selected>' + item.channelname + '</option>';
            } else {
                htmlStr += '<option value="' + item.id + '">' + item.channelname + '</option>';

            }
        }
    }
    else {
        for (var j = 0; j < result.length; j++){
            var authorityItem = result[j];
            htmlStr += '<option value="' + authorityItem.id + '">' + authorityItem.channelname + '</option>';
        }
    }
    return new Handlebars.SafeString(htmlStr);
});

// 互动竞猜列表所属频道select选中判断
Handlebars.registerHelper('isGuessChannel', function(result, options) {
    var htmlStr = '';
    if(result.flag) {
        for (var i = 0; i < result.syChannelinfos.length; i++) {
            var item = result.syChannelinfos[i];
            var flag = false;
            if (result.syGuessinfo.syChannelinfo) {
                var itemJ = result.syGuessinfo.syChannelinfo;
                if (item.id == itemJ.id) {
                    flag = true;
                }
            }
            if (flag) {
                htmlStr += '<option value="' + item.id + '" selected>' + item.channelname + '</option>';
            } else {
                htmlStr += '<option value="' + item.id + '">' + item.channelname + '</option>';

            }
        }
    }
    else {
        for (var j = 0; j < result.length; j++){
            var authorityItem = result[j];
            htmlStr += '<option value="' + authorityItem.id + '">' + authorityItem.channelname + '</option>';
        }
    }
    return new Handlebars.SafeString(htmlStr);
});

// 开启平台多种可能选框选择
Handlebars.registerHelper ('CheckedPlatform', function (result, value) {
    if(result){
        if (result.indexOf(value) > 0 || result.indexOf(value) == 0 ) {
            return 'checked'
        } else {
            return '';
        }
    }
});

// 去除第一个字符，作用于图片路径
Handlebars.registerHelper ('forImg', function (value) {
    if(value){
        return value.substring(1);
    } else{
        return '';
    }
});

// 字符串限制17个
Handlebars.registerHelper ('cutString', function (value) {
    value = (value) ? value+'' : '';
    if(value.length > 10)return value.substring(0, 17)+'…'
        else return value;
});

// 根据timestamp显示 xx分钟前
Handlebars.registerHelper ('timeFromNow', function (timestamp) {
    var now = new Date().getTime();
    var timeDiff = now - timestamp;
    var timeFromNow = '刚刚';
    // 30秒之内显示刚刚
    if(timeDiff < 30 * 1000){
        timeFromNow = '刚刚';
    }
    // 1小时内显示 xx分钟前
    else if(timeDiff < 60 * 60 * 1000){
        timeFromNow = Math.ceil(timeDiff / 1000 / 60) + '分钟前';
    }
    // 24小时内显示 xx小时前
    else if(timeDiff < 24 * 60 * 60 * 1000){
        timeFromNow = Math.ceil(timeDiff / 1000 / 60 / 60) + '小时前';
    }
    // 其他情况写 xx天前
    else {
        timeFromNow = Math.ceil(timeDiff / 1000 / 60 / 60 / 24) + '天前';
    }
    return timeFromNow;
});

// 显示表情
Handlebars.registerHelper ("mccmsShowEmotion", function (value, options) {
    var str = value;
    var matches = value.match(/\[\S+\]/g);
    var emotions = JSON.parse(sessionStorage.getItem('emtion-array'));

    if(matches && matches.length > 0){
        for(var i = 0; i < matches.length; i++){
            var match = matches[i];

            for(var j = 0; j < emotions.length; j++){
                var emotion = emotions[j];

                if(emotion.code == match){
                    str = str.replace(match, '<img src="' + emotion.file + '" alt="' + emotion.name + '" class="emotion__inline-image">');
                }
            }
        }
    }
    return new Handlebars.SafeString(str);
});

// value2存在value则显示
Handlebars.registerHelper ("checkShow", function (value, value2, options) {
    if(value){
        if(value.indexOf(value2) > 0 || value.indexOf(value2) == 0){
            return options.fn(this);
        }
    }
    return options.inverse(this);
});

// 按系统消息类型显示对应文字
Handlebars.registerHelper ("showSystemMessageType", function (value, options) {
    var messageType = 'N/A';

    switch (parseInt(value)) {
        case 1:
            messageType = '预约';
            break;
        case 2:
            messageType = '竞猜';
            break;
        case 3:
            messageType = '充值';
            break;
        case 4:
            messageType = '签到';
            break;
    }


    return messageType;
});

// 消息推送角色select选中判断
Handlebars.registerHelper('isSelectRole', function(result, options) {
    var htmlStr = '';
    if(result.flag) {
        for (var i = 0; i < result.Role.length; i++) {
            var item = result.Role[i];
            var flag = false;
            if (result.pushMess.pushRoles) {
                var itemJ = result.pushMess.pushRoles[0];
                if (item.id == itemJ.id) {
                    flag = true;
                }
            }
            if (flag) {
                htmlStr += '<option value="' + item.id + '" selected>' + item.name + '</option>';
            } else {
                htmlStr += '<option value="' + item.id + '">' + item.name + '</option>';

            }
        }
    }
    else {
        for (var j = 0; j < result.length; j++){
            var roleItem = result[j];
            htmlStr += '<option value="' + roleItem.id + '">' + roleItem.name + '</option>';
        }
    }
    return new Handlebars.SafeString(htmlStr);
});